-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
lib: prefer call() over apply() if argument list is not array
#60796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
lib: prefer call() over apply() if argument list is not array
#60796
Conversation
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #60796 +/- ##
==========================================
+ Coverage 88.53% 88.55% +0.01%
==========================================
Files 703 703
Lines 208260 208252 -8
Branches 40156 40166 +10
==========================================
+ Hits 184393 184421 +28
+ Misses 15881 15832 -49
- Partials 7986 7999 +13
🚀 New features to boost your workflow:
|
Renegade334
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an aside, it'd be interesting to know whether there's any significant performance difference between the bound FunctionPrototypeApply and the native ReflectApply.
Using the following: diff --git a/lib/eslint.config_partial.mjs b/lib/eslint.config_partial.mjs
index b919708a978..457ae91a987 100644
--- a/lib/eslint.config_partial.mjs
+++ b/lib/eslint.config_partial.mjs
@@ -40,2 +40,10 @@ const noRestrictedSyntax = [
},
+ {
+ selector: "CallExpression:matches([callee.type='Identifier'][callee.name='FunctionPrototypeApply'], [callee.type='MemberExpression'][callee.property.type='Identifier'][callee.property.name='apply'][arguments.length=2])",
+ message: 'Use `ReflectApply` instead of %Function.prototype.apply%',
+ },
+ {
+ selector: "CallExpression[callee.type='Identifier'][callee.name='ReflectApply'][arguments.2.type='ArrayExpression']",
+ message: 'Use `FunctionPrototypeCall` to avoid creating an ad-hoc array',
+ },
];I see there are still 33 reported "problems" in |
Why? |
Doesn't land, is all. |
It's probably too soon to assess, the staging branch is many commits behind |
From a brief comparison on Also for the opposite ( node/lib/internal/webstreams/util.js Lines 169 to 178 in b8b4350
Thanks! At least some of them come from specific files that had prototype primordials removed and restricted: node/lib/eslint.config_partial.mjs Lines 513 to 527 in b8b4350
AFAICT it would be okay to suppress this rule or use .call() instead of primordial, but it's better to be separate PR with benchmark CI.
|
All three of
FunctionPrototypeCall,FunctionPrototypeApply, andReflectApplyare used across codebase without clear guidelines on when each should be preferred.The inconsistency gets to the point where identical lines within same file may use different approach:
node/lib/internal/fs/utils.js
Lines 494 to 498 in b8b4350
node/lib/internal/fs/utils.js
Lines 522 to 526 in b8b4350
This PR brings a bit more consistency by using
FunctionPrototypeCallwhenever the argument list is not an already predefined array.In terms of performance, currently
applywith short predefined array of arguments is almost as fast ascallwith predefined variables (i.e. not...argsnorargs[0], args[1], ...), whilecallis 2-3 times faster thanapplythat allocates new array for each call.This probably can become a linter rule.